//
//  AdMedicalRecordsController.swift
//  EyeVisionForPatients
//
//  Created by liuyutian on 2022/8/19.
//  Copyright © 2022 QDSG. All rights reserved.
//

import UIKit
/// 查看是否有就诊记录
class AdMedicalRecordsController: BaseViewController, FamilyViewControllerDelegate {

    public var doctorVm: DoctorDetailViewModel? = nil
    public var fromMyopia: Bool = false
    public var recordId: String = ""
    
    private let advisoryButton = AdMedicalRecordsButton(icon: UIImage(named: "doctor_admedicalrecords_advisory"), titleText: "在线咨询", describeText: "仅咨询无法开具在线处方", backgroundColor: .init(hexString: "#40D4A3"))
    private let recordsButton = AdMedicalRecordsButton(icon: UIImage(named: "doctor_admedicalrecords_records"), titleText: "在线复诊", describeText: "可开具在线处方\n有院内就诊记录的可预约", backgroundColor: .init(hexString: "#4AD5E7"))
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "选择业务类型"
        
        let contentView = UIView()
        view.addSubview(contentView)
        contentView.snp.makeConstraints { make in
            make.centerY.centerX.equalToSuperview()
            make.left.equalTo(60)
            make.right.equalTo(-60)
        }
        
        contentView.addSubview(advisoryButton)
        advisoryButton.snp.makeConstraints { make in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(100)
        }
        advisoryButton.addTarget(self, action: #selector(advisoryButtonAction), for: .touchUpInside)
        
        contentView.addSubview(recordsButton)
        recordsButton.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(advisoryButton.snp.bottom).offset(40)
            make.height.equalTo(advisoryButton)
        }
        recordsButton.addTarget(self, action: #selector(isRecordsButtonAction), for: .touchUpInside)
    }
    
    @objc func advisoryButtonAction() {
        let newAdvisoryVc = NewPMCAdvisoryController(with: doctorVm!, recordId: recordId, fromMyopia: fromMyopia)
        navigationController?.pushViewController(newAdvisoryVc, animated: true)
    }
    
    
    func pushNewAdvisoryVc(_ patientName : String ,_ patientId : String) {
        let newAdvisoryVc = NewPMCAdvisoryController(with: doctorVm!, recordId: recordId, fromMyopia: fromMyopia)
        newAdvisoryVc.isRecords = true
        newAdvisoryVc.patientId = patientId
        newAdvisoryVc.patientName = patientName
        navigationController?.pushViewController(newAdvisoryVc, animated: true)
    }
    

    @objc func isRecordsButtonAction(_ sender: UIButton) {
        let familyVc = FamilyViewController()
        familyVc.delegate = self
        navigationController?.pushViewController(familyVc, animated: true)
    }
    
    
    func familyVc(_ vc: FamilyViewController, didSelected vm: FamilyRowViewModel){
        
        HUDTool.showActivity()
        AdMedicalRecordsViewModel.getPatientMedicalRecords(patientId: vm.patientId) {[weak self] isRecords  in
            if isRecords {
                self?.pushNewAdvisoryVc(vm.name , vm.patientId )
            }else {
                AdMedicalRecordsAlertView.show(status: .failure)
//                HUDTool.showError(message: "暂无医院就诊记录，请先在线咨询")
            }
        } failure: { errorMsg in
            HUDTool.showError(message: errorMsg)
        }

    }
}

class AdMedicalRecordsButton: UIControl {
    init(icon: UIImage?, titleText: String, describeText: String, backgroundColor: UIColor? ) {
        super.init(frame: .zero)
        self.backgroundColor = backgroundColor
        layer.cornerRadius = 16
        layer.shadowRadius = 10
        layer.shadowColor = UIColor.init(hex: 0x000000)?.withAlphaComponent(0.16).cgColor
        layer.shadowOpacity = 1
        layer.shadowOffset = CGSize(width: 0, height: 3)
        
        let iconImage = UIImageView(image: icon)
        addSubview(iconImage)
        iconImage.snp.makeConstraints { make in
            make.left.equalTo(50)
            make.centerY.equalToSuperview()
        }
        
        let view = UIView()
        addSubview(view)
        view.snp.makeConstraints { make in
            make.left.equalTo(iconImage.snp.right).offset(15)
            make.centerY.equalToSuperview()
            make.right.equalTo(-20)
        }
        view.isUserInteractionEnabled = false
        
        let title = UILabel()
        view.addSubview(title)
        title.snp.makeConstraints { make in
            make.left.top.equalToSuperview()
        }
        title.text = titleText
        title.font = .bold(16)
        title.textColor = .white
        
        let describe = UILabel()
        view.addSubview(describe)
        describe.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(title.snp.bottom).offset(5)
        }
        describe.numberOfLines = 0
        describe.font = .regular(12)
        describe.textColor = .white
        describe.text = describeText
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

class AdMedicalRecordsAlertView: UIView {
    
    class func show(status: AdMedicalRecordsAlertView.Status = .failure) {
        let view = AdMedicalRecordsAlertView(status: status)
        UIApplication.keyWindows()?.addSubview(view)
        view.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    enum Status {
        case failure
        
        var image: UIImage? {
            switch self {
            case .failure: return UIImage(named: "doctor_admedicalrecords_status_failure")
            }
        }
        var title: String {
            switch self {
            case .failure: return "查询失败"
            }
        }
        var describe: String {
            switch self {
            case .failure: return "未查询到您在院内就诊记录\n请选择其他业务方式"
            }
        }
    }
    
    init(status: AdMedicalRecordsAlertView.Status = .failure) {
        super.init(frame: .zero)
        backgroundColor = .white.withAlphaComponent(0.25)
        
        let contentView = UIView()
        addSubview(contentView)
        contentView.snp.makeConstraints { make in
            make.centerY.centerX.equalToSuperview()
            make.left.equalTo(50)
            make.right.equalTo(-50)
        }
        contentView.layer.cornerRadius = 14
        contentView.layer.masksToBounds = true
        contentView.backgroundColor = .white
        
        let icon = UIImageView(image: status.image)
        contentView.addSubview(icon)
        icon.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(30)
        }
        
        let title = UILabel()
        contentView.addSubview(title)
        title.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(icon.snp.bottom).offset(25)
        }
        title.text = status.title
        title.font = .medium(16)
        
        let describe = UILabel()
        contentView.addSubview(describe)
        describe.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.top.equalTo(title.snp.bottom).offset(10)
        }
        describe.font = .regular(12)
        describe.text = status.describe
        describe.textColor = .textLight
        
        let complete = UIButton()
        contentView.addSubview(complete)
        complete.snp.makeConstraints { make in
            make.left.bottom.right.equalToSuperview()
            make.height.equalTo(44)
            make.top.equalTo(describe.snp.bottom).offset(10)
        }
        complete.titleLabel?.font = .medium(16)
        complete.setTitle("返回", for: .normal)
        complete.setTitleColor(.main, for: .normal)
        complete.addTarget(self, action: #selector(completeClick), for: .touchUpInside)
    }
    
    @objc func completeClick() {
        removeFromSuperview()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
